package com.example.hellonotes.adapter;

import java.util.ArrayList;

import com.example.hellonotes.R;
import com.example.hellonotes.activity.ImageBrowserActivity;
import com.example.hellonotes.activity.Select2Activity;
import com.example.hellonotes.activity.selectActivity;
import com.example.hellonotes.entity.Person2;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class NotesBmobAdapter extends BaseAdapter{

	private LinearLayout layout;
	private Context context;
	private ArrayList<Person2> list;
	
	
	
	public NotesBmobAdapter(Context context,ArrayList<Person2> list) {
		// TODO 自动生成的构造函数存根
		this.context=context;
		this.list = list;
	}
	
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO 自动生成的方法存根
		LayoutInflater inflater = LayoutInflater.from(context);
		final Person2 entity =list.get(position);
		layout = (LinearLayout) inflater.inflate(R.layout.cell2, null);
		LinearLayout cell_content = (LinearLayout)layout.findViewById(R.id.cell_content);
		TextView contentTV = (TextView) layout.findViewById(R.id.list_content);
		TextView timeTV = (TextView) layout.findViewById(R.id.list_time);
		ImageView imgIV = (ImageView) layout.findViewById(R.id.list_img);
		TextView userTV = (TextView)layout.findViewById(R.id.list_user);
		final String content = entity.getContent();
		final String path = entity.getPath().getFileUrl(context);
		final String time = entity.getCreatedAt();
		final String user = entity.getUser().getUsername();
		contentTV.setText(content);
		timeTV.setText(time);
		userTV.setText(user);
		ImageLoader.getInstance().displayImage(path+"", imgIV);
		
		imgIV.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				Intent intent = new Intent(context, ImageBrowserActivity.class);
				intent.putExtra("entity", entity);
				//intent.putExtra("position", position);
				context.startActivity(intent);
				
			
			}
		});
		
		/**
		 * 内容的点击事件
		 */
		cell_content.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				/*Intent intent = new Intent(context, Select2Activity.class);
				intent.putExtra("entity", entity);
				context.startActivity(intent);*/	
				
				Intent intent = new Intent(context, selectActivity.class);
				intent.putExtra("entity", entity);
				context.startActivity(intent);
			}
		});
		
		return layout;	
		
	}



	@Override
	public int getCount() {
		// TODO 自动生成的方法存根
		return list.size();
	}


	@Override
	public Object getItem(int position) {
		// TODO 自动生成的方法存根
		return list.get(position);
	}


	@Override
	public long getItemId(int position) {
		// TODO 自动生成的方法存根
		return position;
	}

	public static class ViewHolder {
		public LinearLayout cell_content;
	}
	
	
}
